#include "Adafruit_SH1106.h"
#include "MyDisplay.h"
#include "MAX31865/MyTemperature.h"
#include "my_font.c"

MyDisplay::MyDisplay() : otherTemperature(-300)
{
    _display = new Adafruit_SSD1306();
    _display->begin(); //初始化显示器

    memset(raw_all, 0, sizeof(raw_all));
    memset(temperature_all, 0, sizeof(temperature_all));
}

MyDisplay::~MyDisplay()
{
    delete _display;
}

void MyDisplay::welcome()
{
    _display->clearDisplay(); //清屏
    _display->setTextSize(2);
    _display->setTextColor(WHITE); //白字
    _display->setCursor(2, 10);    //x=0-131,y=0-63
    _display->print("Welcome");
    _display->display(); //刷新屏幕
}

void MyDisplay::setData(uint8_t senserNumber, uint16_t raw, double temperature)
{
    this->raw_all[senserNumber] = raw;
    this->temperature_all[senserNumber] = temperature;
}

void MyDisplay::display(uint8_t mode)
{
    switch (mode)
    {
    case MODE_RUN:
        displayRun();
        break;

    // case MODE_SET_RAW_0:
    //     displaySetRaw0();
    //     break;

    // case MODE_SET_RAW_100:
    //     displaySetRaw100();
    //     break;

    case MODE_SET_OTHER:
        displaySetOthers();
        break;

    default:
        displayRun();
        break;
    }
}

void MyDisplay::displayCN(int x, int y, const unsigned char *CN, int start, int length)
{
    for (size_t l = 0; l < length; l++) //两个字
    {
        for (size_t k = 0; k < 2; k++) //每个字有两行（16/8）
        {
            for (size_t i = 0; i < 16; i++) //每个字有16列
            {
                unsigned char c = *(CN + i + k * 16 + l * 32 + start * 32);
                for (size_t j = 0; j < 8; j++) //每个数据是8个点
                {
                    if (c & 0x1)
                    {
                        //_display->drawPixel(x + i + l * 16, y + j + k * 8, BLACK);
                    }
                    else
                    {
                        _display->drawPixel(x + i + l * 16, y + j + k * 8, WHITE);
                    }
                    c >>= 1;
                }
            }
        }
    }
}

//正常运行模式
void MyDisplay::displayRun()
{
    _display->clearDisplay(); //清屏

    displayCN(2, 0, CN_RUN, 0, 2); //显示汉字

    displayTemperature(); //显示温度
}

void MyDisplay::displayTemperature()
{

    int half = SENSER_QUANTITY / 2;
    if (half > 6)
        half = 6; //最多6行,两行标题
    _display->setTextSize(1);
    _display->setTextColor(WHITE); //白字

    int x, y;

    for (size_t i = 0; i < SENSER_QUANTITY; i++)
    {

        if (i < half)
        {
            x = 2;
            y = i * 8 + 16;
        }
        else
        {
            x = 68;
            y = (i - half) * 8 + 16;
        }
        _display->setCursor(x, y); //x=0-131,y=0-63

        uint16_t raw = raw_all[i];
        if (0 == raw)
        {
            _display->printf("T%u=", i);
            continue; //没有，跳过
        }

        //_display->setTextColor(BLACK, WHITE); //黑字
        _display->printf("T%u=", i);
        /////四舍五入保留1位
        int t2 = rint(temperature_all[i] * 10);
        _display->print(t2 / 10);
        _display->print(".");
        t2 = abs(t2); //负数情况
        _display->print(t2 % 10);
        _display->print(" C");
    }

    _display->display(); //刷新屏幕
}

// //标定0度
// void MyDisplay::displaySetRaw0()
// {
//     _display->clearDisplay(); //清屏

//     displayCN(2, 0, CN_SET, 0, 2); //显示汉字“设置”

//     _display->setTextSize(2);
//     _display->setTextColor(BLACK, WHITE); //黑字
//     _display->setCursor(34, 0);           //x=0-131,y=0-63
//     _display->print(" 0 C");

//     displayRaw(); //显示raw
// }

// //标定100度
// void MyDisplay::displaySetRaw100()
// {
//     _display->clearDisplay(); //清屏

//     displayCN(2, 0, CN_SET, 0, 2); //显示汉字

//     _display->setTextSize(2);
//     _display->setTextColor(BLACK, WHITE); //黑字
//     _display->setCursor(34, 0);           //x=0-131,y=0-63
//     _display->print(" 100 C");

//     displayRaw();
// }

//标定
void MyDisplay::displaySetOthers()
{
    _display->clearDisplay();      //清屏
    displayCN(2, 0, CN_SET, 0, 3); //显示汉字
    if (otherTemperature < -280)
    { //未曾修改，求平均数
        int count = 0;
        double a = 0.0;
        for (size_t i = 0; i < SENSER_QUANTITY; i++)
        {
            uint16_t raw = raw_all[i];
            if (0 != raw && temperature_all[i] > 0)
            {
                a += temperature_all[i];
                count++;
            }
        }

        if (count > 0)
        {
            otherTemperature = a / count;
            //消除小数点尾巴
            int b = (int)round(otherTemperature * 10);
            otherTemperature = (double)b / 10;
        }
        else
        {
            otherTemperature = 0;
        }

        if (otherTemperature > 300 || otherTemperature < 0)
        {
            otherTemperature = 0;
        }
    }

    _display->setTextSize(2);
    _display->setTextColor(BLACK, WHITE); //黑字
    _display->setCursor(50, 0);           //x=0-131,y=0-63

    _display->print(" ");

    /////四舍五入保留1位
    int t2 = rint(this->otherTemperature * 10);
    _display->print(t2 / 10);
    _display->print(".");
    t2 = abs(t2); //负数情况
    _display->print(t2 % 10);
    //_display->print("C");

    displayTemperature();
}

//显示raw
void MyDisplay::displayRaw()
{
    int x, y;
    _display->setCursor(34, 0); //x=0-131,y=0-63
    _display->setTextSize(1);
    _display->setTextColor(WHITE); //白字

    int half = SENSER_QUANTITY / 2;
    if (half > 6)
        half = 6; //最多6行(两行被标题占用)

    for (size_t i = 0; i < SENSER_QUANTITY; i++)
    {
        if (i < half)
        {
            x = 2;
            y = i * 8 + 16;
        }
        else
        {
            x = 68;
            y = (i - half) * 8 + 16;
        }
        _display->setCursor(x, y); //x=0-131,y=0-63
        _display->printf("R%u=", i);
        _display->print(raw_all[i]);
    }

    _display->display(); //刷新屏幕
}
